'use strict';

const Controller = require('egg').Controller;

var WXBizDataCrypt = require('../util/WXBizDataCrypt')
var appId = 'wx3a6ab4334aa1402b'
var secret = 'ce9db2ccde818501614452bb4ded4400'

const {
  ERROR,
  SUCCESS,
} = require('../util/util');

class UserController extends Controller {
  async login() {
      const { ctx, logger } = this
      const { mobilePhone, 
              openId, 
              username, 
              avatarUrl,
              gender
            } = ctx.request.body
      
      logger.info("/user/login 入参======>>" + JSON.stringify(ctx.request.body))

      ctx.body = await ctx.service.user.login(
        mobilePhone,
        openId,
        username, 
        avatarUrl,
        gender
      )

      logger.info("/user/login 出参======>>" + JSON.stringify(ctx.body))
  }

  async registered() {
    const { ctx, logger } = this
      const { mobilePhone, openId, username, sex, headPhoto } = ctx.request.body
      
      logger.info("/user/registered ======>>" + JSON.stringify(ctx.request.body))

      ctx.body = await ctx.service.user.registered(
        mobilePhone,
        openId,
        username,
        sex,
        headPhoto
    )

    logger.info("/user/registered 出参======>>" + JSON.stringify(ctx.body))
  }

  async refreshUser() {
    const { ctx, logger } = this
      const { username, headPhoto, sex } = ctx.request.body

      logger.info("/user/refreshUser 入参======>>" + JSON.stringify(ctx.request.body))
      
      ctx.body = await ctx.service.user.refreshUser(
        username, 
        headPhoto, 
        sex
    )

    logger.info("/user/refreshUser 出参======>>" + JSON.stringify(ctx.body))
  }

  async getOpenId() {
      const { ctx, logger, app } = this
      const { js_code } = ctx.request.body

      logger.info("/user/getOpenId 入参======>>" + JSON.stringify(ctx.request.body))

      const result = await app.curl('https://api.weixin.qq.com/sns/jscode2session', {
        method: 'GET',
        data: {
          'js_code': js_code,
          'appid': appId,
          'secret': secret,
          'grant_type': 'authorization_code'
        },
        dataType: "json",
        header: {
          'content-type': 'application/json;charset=UTF-8' // 默认值
        },
      })

      if (result.data) {
        ctx.body = Object.assign(SUCCESS(), {
          data: Object.assign(result.data),
        })
      }else {
        ctx.body = Object.assign(ERROR(), {
          msg: 'openId获取失败',
        });
      }

      logger.info("/user/getOpenId 出参======>>" + JSON.stringify(ctx.body))
  }

  async checkJsCode() {
    const { ctx, logger } = this

    logger.info("/user/checkJsCode 入参======>>" + JSON.stringify(ctx.request.body))

    const { encryptedData, iv, sessionKey } = ctx.request.body

    try {
      var pc = new WXBizDataCrypt(appId, sessionKey)

      var data = pc.decryptData(encryptedData , iv)

      ctx.body = Object.assign(SUCCESS(), {
        data: Object.assign(data),
      })

      logger.info("/user/checkJsCode 出参======>>" + JSON.stringify(ctx.body))
    } catch (error) {
      logger.error(error)
      ctx.body = Object.assign(ERROR(), {
        msg: '手机号获取失败',
      });
    }
  }
}

module.exports = UserController;
